// seed-test-data.js
// 用于生成测试数据的脚本

const mysql = require('mysql2/promise')

async function seedTestData() {
  let connection
  try {
    // 创建数据库连接
    connection = await mysql.createConnection({
      host: 'localhost',
      user: 'root',
      password: '888mei525',
      database: 'lingxi_system'
    })
    
    console.log('数据库连接成功')
    
    // 创建测试客户
    console.log('正在创建测试客户...')
    const customers = []
    for (let i = 1; i <= 10; i++) {
      const [result] = await connection.execute(
        `INSERT IGNORE INTO customers (name, contact_persons, status, created_at) 
         VALUES (?, ?, 1, NOW())`,
        [`测试客户${i}`, JSON.stringify([{name: `联系人${i}`, phone: `1380013800${i}`, role: '主要联系人'}])]
      )
      
      // 获取客户ID
      const [rows] = await connection.execute(
        'SELECT id FROM customers WHERE name = ?',
        [`测试客户${i}`]
      )
      
      customers.push({
        id: rows[0].id,
        name: `测试客户${i}`
      })
    }
    
    console.log(`创建了 ${customers.length} 个测试客户`)
    
    // 创建银行流水数据 (20条)
    console.log('正在创建银行流水数据...')
    for (let i = 1; i <= 20; i++) {
      await connection.execute(
        `INSERT IGNORE INTO bank_transactions 
         (transaction_id, payer_name, payer_account, amount, transaction_date, transaction_time, balance, remark, data_source, import_batch, is_filtered, created_at)
         VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())`,
        [
          `TXN${String(i).padStart(3, '0')}`,
          customers[i % customers.length].name,
          `ACC${String(i).padStart(4, '0')}`,
          Math.random() * 10000 + 1000, // 1000-11000之间的随机金额
          new Date(Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000).toISOString().split('T')[0], // 最近30天内的随机日期
          new Date(Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000).toISOString().slice(0, 19).replace('T', ' '), // 随机时间
          Math.random() * 50000 + 10000, // 10000-60000之间的随机余额
          `测试流水备注${i}`,
          'file_import',
          'TEST_BATCH_001',
          0 // isFiltered = false
        ]
      )
    }
    
    console.log('创建了 20 条银行流水')
    
    // 创建销售订单数据 (30条)
    console.log('正在创建销售订单数据...')
    for (let i = 1; i <= 30; i++) {
      await connection.execute(
        `INSERT IGNORE INTO sales_orders 
         (platform_order_id, customer_id, order_amount, order_status, order_date, platform_source, invoice_status, invoice_date, responsible_salesman, created_at)
         VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())`,
        [
          `PO${String(i).padStart(3, '0')}`,
          customers[i % customers.length].id,
          Math.random() * 10000 + 1000, // 1000-11000之间的随机金额
          'completed',
          new Date(Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000).toISOString().split('T')[0], // 最近30天内的随机日期
          i % 2 === 0 ? 'zcy' : 'manual',
          'invoiced',
          new Date(Date.now() - Math.random() * 15 * 24 * 60 * 60 * 1000).toISOString().split('T')[0], // 发票日期
          `销售员${i % 5 + 1}`
        ]
      )
    }
    
    console.log('创建了 30 条销售订单')
    
    console.log('测试数据创建完成!')
    await connection.end()
    process.exit(0)
  } catch (error) {
    console.error('创建测试数据失败:', error)
    if (connection) {
      await connection.end()
    }
    process.exit(1)
  }
}

// 执行数据填充
seedTestData()